<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/about' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <el-card style="margin-top: 20px">
      <!-- 输入内容     添加商品 -->
      <el-row>
        <el-col :span="6">
          <el-input
            placeholder="请输入内容"
            v-model="keyword"
            class="input-with-select"
          >
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button
            style="margin-left: 20px"
            type="primary"
            @click="$router.push('/goods/add')"
            >添加商品</el-button
          >
          <!-- <router-link to="/goods/add">
              <el-button style="margin-left: 20px" type="primary" >添加商品</el-button>
          </router-link> -->
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table :data="tableData" border style="width: 100%; margin-top: 20px">
        <el-table-column prop="goods_id" label="" width="60"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="420">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="110">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="90">
        </el-table-column>
        <el-table-column prop="goods_number" label="商品数量" width="90">
        </el-table-column>
        <el-table-column label="创建时间" width="160">
          <template slot-scope="scope">
            <div>
              {{ scope.row.add_time | timeForment }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <!-- <router-view></router-view> -->
    </el-card>
  </div>
</template>

<script>
export default {
  name: "goods",
  data() {
    return {
      keyword: "",
      tableData: [],
      pagesize: 20,
      pagenum: 2,
      total: 0,
    };
  },
  mounted() {
    this.getdata();
  },
  methods: {
    // 删除
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // console.log(row);
          let res = await this.$axios({
            url: `/api/private/v1/goods/${row.goods_id}`,
            method: "delete",
          }).catch((err) => err); // console.log(res);
          this.$message.success("删除成功!");
          this.getdata();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 分页
    // 每页显示的条数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pagesize = val;
      this.getdata();
    },
    // 当前页数
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      (this.pagenum = val), this.getdata();
    },
    // 渲染接口
    async getdata() {
      var resule = await this.$axios({
        url: "/api/private/v1/goods",
        method: "get",
        params: {
          query: this.keyword,
          pagenum: this.pagenum,
          pagesize: this.pagesize,
        },
      }).catch((err) => console.log(err));
      console.log(resule.data, "goods");
      this.tableData = resule.data.data.goods;
      this.total = resule.data.data.total;
    },
  },
};
</script>

<style lang="scss" scoped>
</style>